// webpack plugins
var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var CommonsChunkPlugin = require("webpack/lib/optimize/CommonsChunkPlugin");

// import {
//   AotPlugin
// } from '@ngtools/webpack';

let {
    AotPlugin
} = require("@ngtools/webpack");

/**
 * config
 * Reference: http://webpack.github.io/docs/configuration.html
 */
module.exports = {
    // app entry
    entry: {
        app: 'main.ts',
        // polyfills: 'polyfills.ts',
        vendor: ['polyfills.ts', 'rxjs-entry.ts', 'vendor.ts'],
        vendorOfThem: ['articleVendor.ts'],
        webim: "webim-entry.ts",
        rxjsEntry: "rxjs-entry.ts",
        readArticle: "read-article.ts",
    },
    resolve: {
        modules: [
            path.resolve(__dirname, 'src'),
            "node_modules"
        ],
        extensions: ['.webpack.js', '.web.js', '.ts', '.tsx', '.js', '.html', '.less'],
        alias: {
            "tschart": "lib/tschart",
            "CmHttp": "http-module",
            "utils": "shared"
        }
    },
    module: {
        rules: [{
                test: /\.ts$/,
                // loaders: ['ts-loader', 'angular2-template-loader'],
                use: [{
                        loader: "awesome-typescript-loader",
                        options: {
                            // useBabel: true,
                            transpileOnly: true,
                            useCache: true
                        }
                    },
                    'angular2-template-loader',
                    {
                        loader: 'string-replace-loader',
                        options: {
                            multiple: [{
                                search: 'test\.58caimi\.com',
                                replace: 'hrhg.integrity.com.cn',
                                flags: 'ig'
                            }, {
                                search: 'www\.58caimi\.com',
                                replace: 'hrhg.integrity.com.cn',
                                flags: 'ig'
                            }, {
                                search: 'ogvk6eo7a\.bkt\.clouddn\.com',
                                replace: 'images.integrity.com.cn',
                                flags: 'ig'
                            }]
                        }
                    }
                    // "@ngtools/webpack"
                ],
                enforce: "pre"
            },
            {
                test: /\.js$/,
                // loaders: ['babel?presets[]=es2015'],
                exclude: /node_modules/,
                loader: "babel-loader",
                options: {
                    presets: ["es2015-ie"]
                }
            },
            {
                test: /\.html$/,
                loader: 'html-loader',
                exclude: path.resolve(__dirname, "./src/index.html")
            },
            {
                test: /\.template$/,
                loader: "file-loader"
            }, {
                test: /\.less$/,
                // loader: 'raw!less',
                exclude: path.resolve(__dirname, './src/app'),
                use: [
                    "raw-loader",
                    "less-loader"
                ],
            },
            {
                test: /\.css$/,
                // loader: 'style!css',
                use: [
                    "style-loader",
                    "css-loader"
                ]
            },
            {
                test: /\.less$/,
                // loader: ExtractTextPlugin.extract('css!less'),
                use: ExtractTextPlugin.extract({
                    use: [
                        "css-loader",
                        "less-loader"
                    ]
                }),
                include: path.resolve(__dirname, './src/app')
            },
            {
                test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
                loader: 'url-loader',
                options: {
                    limit: 8192
                }
            }
        ],
    },
    plugins: [
        new CommonsChunkPlugin({
            names: ['vendor', 'vendorOfThem'],
            minChunks: Infinity,
            filename: "scripts/[name].js"
        }),
        // new CommonsChunkPlugin({
        //     name: ['vendor'],
        //     minChunks: Infinity,
        //     filename: "scripts/vendor.js"
        // }),
        // new CommonsChunkPlugin({
        //   name: "readArticle",
        // }),
        new CopyWebpackPlugin([{
            from: './assets',
            to: 'static'
        }]),
        new HtmlWebpackPlugin({
            template: './src/index.html',
            filename: './index.html',
            chunks: ["vendorOfThem", "vendor", "rxjsEntry", 'app'],
            // children: true
            chunksSortMode: (pre, next) => {
                let order = {
                    // "browser": 1,
                    "vendorOfThem": 0,
                    "vendor": 1,
                    "rxjsEntry": 2,
                    "webim": 3,
                    "app": 4
                };

                if (order[pre.names[0]] - order[next.names[0]] > 0) return 1
                if (order[pre.names[0]] - order[next.names[0]] < 0) return -1

                return 0;
            }
        }),
        new HtmlWebpackPlugin({
            template: './src/read-article.html',
            filename: './read-article.html',
            // chunksSortMode: 'dependency',
            chunks: ['readArticle', "vendorOfThem"],
            chunksSortMode: (pre, next) => {
                let order = {
                    // "browser": 1,
                    "vendorOfThem": 0,
                    "readArticle": 1
                };

                if (order[pre.names[0]] - order[next.names[0]] > 0) return 1
                if (order[pre.names[0]] - order[next.names[0]] < 0) return -1

                return 0;
            },
            children: true
        }),
        new CopyWebpackPlugin([{
            from: 'src/refresh.html',
            to: ''
        }]),
        new webpack.NamedModulesPlugin(),
        // new AotPlugin({
        //   tsConfigPath: './tsconfig-aot.json',
        //   // "mainPath": "src/main.ts",
        //   // skipCodeGeneration: true
        //   entryModule: 'src/app.module'
        // })
    ],
    externals: [{
        "webim": "webim"
    }, ]
};